//
//  AccountOrderListVC.m
//  ImGuider
//
//  Created by llt on 2017/5/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountOrderListVC.h"

#import "AccountOrderListCell.h"
#import "LinePreDetailVC.h"
#import "ContactService.h"

#import "RatingVC.h"

@interface AccountOrderListVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

//@property (nonatomic) NSInteger start;

@end

@implementation AccountOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"ACCOUNT_ORDER", nil);
    
    [self configEmptyDataTableView:self.tableView emptyImageName:@"暂无订单" describe:MYLocalizedString(@"PUBLIC_NODATA_ORDER", nil) refresh:nil block:nil];
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
//        self.start = 0;
        [self.tableView.mj_footer resetNoMoreData];
        [self loadDataSource];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self loadDataSource];
//    }];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];
}

- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)loadDataSource {

    @weakify(self);
    [NetAPIManager getOrderStart:@(0) count:@(1000) Block:^(NSArray<OrderModel *> *orderArray, NSError *error) {
        @strongify(self);
        
        self.dataHasLoad = YES;
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [self.tableView.mj_header endRefreshing];
        
        if (orderArray.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            
            [self.tableView.mj_footer endRefreshing];
        }
        
        self.dataSource = [NSMutableArray arrayWithArray:orderArray];
        
//        if (self.start == 0) {
//            self.dataSource = [NSMutableArray arrayWithArray:orderArray];
//
//            if (orderArray.count < 10) {
//                self.tableView.mj_footer = nil;
//            } else {
//                self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//                    @strongify(self);
//                    [self loadDataSource];
//                }];
//            }
//
//        } else {
//            [self.dataSource addObjectsFromArray:orderArray];
//        }
        
        [self.tableView reloadData];
        
//        self.start ++;
    }];
}

#pragma mark -UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifi = @"cell1";
    
    
    OrderModel *order = self.dataSource[indexPath.section];
    
    if (order.orderstatus.integerValue == -1) {// 失败
        identifi = @"cell2";
    }
    
    AccountOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifi];
    
    cell.evaluateBtn.tag = indexPath.section;
    cell.contactServiceBtn.tag = indexPath.section;
    [cell.evaluateBtn addTarget:self action:@selector(evaluateAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contactServiceBtn addTarget:self action:@selector(contactServiceAction:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.order = order;
    
    return cell;
}

- (void)evaluateAction:(UIButton *)sender {
    
    OrderModel *order = self.dataSource[sender.tag];
    
    RatingVC *rateVC = [RatingVC new];
    
    [rateVC show:self completeBlock:^(CGFloat score, NSString *comment) {
        
        [NetAPIManager evaluate:order.lineid score:@(score) comment:comment Block:^(NSString *result, NSError *error) {
            
            if (!error) {
                [IGUtils showToast:MYLocalizedString(@"PUBLIC_COMMIT_SUCCESS", nil)];
                order.score = @(score).stringValue;
                [self.tableView reloadData];
            }
        }];
    }];
    
}
- (void)contactServiceAction:(UIButton *)sender {
    
    UIViewController *contactService = [IGUtils loadViewController:@"ContactService" storyboard:@"Account"];
    [self.navigationController pushViewController:contactService animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    OrderModel *order = self.dataSource[indexPath.section];
    
    
    LinePreDetailVC *detailVC = [IGUtils loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    detailVC.lineid = order.lineid;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
@end
